"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 基础功能
Case Name   : 修改模式名称,该模式下的带有序列字段的表结构正确
Create At   : 2023/02/11
Owner       : @kyeleze
Description :
    1.创建schema
    2.创建带有序列字段的表
    3.\d+查询表结构
    4.重命名schema
    5.\d+查询表结构
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
History     :
"""

import os
import unittest

from yat.test import Node

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Ddlschema(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.constant = Constant()
        self.commonsh = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        self.table_name = "t_ddl_table_012"
        self.schema_name = "t_ddl_schema_012"
        self.schema_name_bak = "t_ddl_schema_012_bak"
        self.user_node = Node('PrimaryDbUser')
        self.user_param = f'-U {self.user_node.db_user} ' \
                          f'-W {self.user_node.db_password}'
        self.log.info("-----检查数据库状态是否正常-----")
        status = self.commonsh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_schema(self):
        text = "--step01:创建schema expect:成功--"
        self.log.info(text)
        sql_cmd = f'drop schema if exists {self.schema_name} cascade;' \
                  f'create schema {self.schema_name};'
        self.log.info(sql_cmd)
        sql_result = self.commonsh.execut_db_sql(sql_cmd,
                                                 sql_type=self.user_param)
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_SCHEMA_SUCCESS_MSG,
                      sql_result, "执行失败:" + text)

        text = "--step02:创建带有序列字段的表 expect:成功--"
        self.log.info(text)
        sql_cmd = f'drop table if exists ' \
                  f'{self.schema_name}.{self.table_name};' \
                  f'create table {self.schema_name}.{self.table_name}' \
                  f'(cn_a int, cn_b text , cn_c date ,cn_d interval, ' \
                  f'cn_e serial);'
        self.log.info(sql_cmd)
        sql_result = self.commonsh.execut_db_sql(sql_cmd,
                                                 sql_type=self.user_param)
        self.log.info(sql_result)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS,
                      sql_result, "执行失败:" + text)

        text = "--step03:\d+查询表结构 expect:成功--"
        self.log.info(text)
        sql_cmd = f'\\d+ {self.schema_name}.{self.table_name}'
        self.log.info(sql_cmd)
        sql_result = self.commonsh.execut_db_sql(sql_cmd,
                                                 sql_type=self.user_param)
        self.log.info(sql_result)
        self.assertIn(self.table_name, sql_result, "执行失败:" + text)

        text = "--step04:重命名schema expect:成功--"
        self.log.info(text)
        sql_cmd = f'alter schema {self.schema_name} rename to ' \
                  f'{self.schema_name_bak};'
        self.log.info(sql_cmd)
        sql_result = self.commonsh.execut_db_sql(sql_cmd,
                                                 sql_type=self.user_param)
        self.log.info(sql_result)
        self.assertIn(self.constant.ALTER_SCHEMA_SUCCESS_MSG,
                      sql_result, "执行失败:" + text)

        text = "--step05:\d+查询表结构 expect:成功--"
        self.log.info(text)
        self.log.info(text)
        sql_cmd = f'\\d+ {self.schema_name_bak}.{self.table_name}'
        self.log.info(sql_cmd)
        sql_result = self.commonsh.execut_db_sql(sql_cmd,
                                                 sql_type=self.user_param)
        self.log.info(sql_result)
        self.assertIn(self.table_name, sql_result, "执行失败:" + text)

    def tearDown(self):
        text = "----清理环境---"
        self.log.info(text)
        clean_result = self.commonsh.execut_db_sql(
            f'drop schema {self.schema_name_bak} cascade;',
            sql_type=self.user_param)
        self.log.info(clean_result)
        self.assertIn(self.constant.DROP_SCHEMA_SUCCESS_MSG,
                      clean_result, "执行失败:" + text)
        self.log.info(f"-----{os.path.basename(__file__)} end-----")

